use actix_web::{web,App,HttpServer};
//use sqlx::Pool;
use std::sync::Mutex;
use std::io;
use mysql::*;
use mysql::prelude::*;
//use dotenv::dotenv;
//use std::env;
//use sqlx::postgres::PgPoolOptions;
use routers::*;
use states::AppState;
mod handlers;
mod routers;
mod states;
mod models;
mod db_access;
//实例化 HTTP sever 并运行
#[actix_rt::main]
async fn main() -> io::Result<()>{
    //构建mysql连接池
    //dotenv().ok();//获取环境变量
    //let url = env::var("DATABASE_URL").expect("DATABASE_URL is not set!");
    //let db_pool = PgPoolOptions::new().connect(&database_url).await.unwrap();

    let url = "mysql://root:1234@localhost:3306/productDB";
    let opts = Opts::from_url(url).unwrap();
    let pool = Pool::new(opts).unwrap();//获取连接池
    let mut db = pool.get_conn().unwrap();
    //构建app，配置route
    let shared_data = web::Data::new(AppState{
        health_check_response: "The App is ready!".to_string(),
        visit_count : Mutex::new(0),
        db : pool,
    });
    let app = move || App::new()
        .app_data(shared_data.clone())
        .configure(general_routes)
        .configure(product_routes);

    HttpServer::new(app).bind("127.0.0.1:3000")?.run().await
}


